package view.component.menu
{
import core.Language;
import core.manager.MapBuildingManager;
import core.model.MapModel;
import core.model.PlayerModel;

import flash.events.Event;
import flash.events.MouseEvent;

import framework.view.components.Component;
import framework.view.components.button.ButtonProxy;

import mapengine.interfaces.IMapBuilding;

/**
 * MapBuildingMenu
 * @author jliang
 *
 */
public class MapBuildingMenu extends Component
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function MapBuildingMenu()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	[Inject]
	public var playerModel:PlayerModel;
	
	[Inject]
	public var mapModel:MapModel;
	
	protected var _data:IMapBuilding;
	
	public function get data():IMapBuilding
	{
		return _data;
	}
	
	public function set data(value:IMapBuilding):void
	{
		_data = value;
		
		init();
	}
	
	protected function get upgradeBtn():ButtonProxy
	{
		return proxys["upgradeBtn"];
	}
	
	protected function get stashBtn():ButtonProxy
	{
		return proxys["stashBtn"];
	}
	
	protected function get moveBtn():ButtonProxy
	{
		return proxys["moveBtn"];
	}
    
    protected function get rotateBtn():ButtonProxy
    {
        return proxys["rotateBtn"];
    }
	
	protected function get sellBtn():ButtonProxy
	{
		return proxys["sellBtn"];
	}
	//==========================================================================
	//  Overridden methods: MapBuildingMenu
	//==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"upgradeBtn", classRef: ButtonProxy, id:"upgradeBtn", text:Language.getText("@building_menu_upgrade_button_label"), onClick:clickToUpgradeHandler },
			{ skin:"stashBtn", classRef: ButtonProxy, id:"stashBtn", text:Language.getText("@building_menu_stash_button_label"), onClick:clickToStashHandler },
			{ skin:"moveBtn", classRef: ButtonProxy, id:"moveBtn", text:Language.getText("@building_menu_move_button_label"), onClick:clickToMoveHandler },
            { skin:"rotateBtn", classRef: ButtonProxy, id:"rotateBtn", text:Language.getText("@building_menu_rotate_button_label"), onClick:clickToRotateHandler },
			{ skin:"sellBtn", classRef: ButtonProxy, id:"sellBtn", text:Language.getText("@building_menu_sell_button_label"), onClick:clickToSellHandler },
		];
	}
	
	override public function initialize():void
	{
		super.initialize();
		
		if (stage)
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		else
		{
			this.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		}
	}
	
	override public function dispose():void
	{
		this.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		
		if (stage)
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		if (parent)
		{
			parent.removeChild(this);
		}
		
		super.dispose();
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function init():void
	{
		if (!data.canUpgrade)
		{
			upgradeBtn.enabled = false;
		}
	}
	
	protected function clickToUpgradeHandler():void
	{
		if (!data.canUpgrade) return;
	}
	
	protected function clickToStashHandler():void
	{
		if (!data.canStash || !playerModel.hasEnoughSpace(data.buildingItemValue)) return;
		
		MapBuildingManager.stash(data);
	}
	
	protected function clickToMoveHandler():void
	{
		if (!data.canMove) return;
		
		MapBuildingManager.move(data);
	}
    
    protected function clickToRotateHandler():void
    {        
        MapBuildingManager.rotate(data);
    }
	
	protected function clickToSellHandler():void
	{
		if (!data.canSell) return;
        
        MapBuildingManager.sell(data);
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function addToStageHandler(event:Event):void
	{
		event.target.removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
	}
	
	protected function mouseUpHandler(event:MouseEvent):void
	{
		event.target.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		
		dispose();
	}
}
}



